<template>
  <div>
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <!-- <el-form-item label="设备大类ID" prop="deviceTypeId">
        <el-input v-model="queryParams.deviceTypeId" placeholder="请输入设备大类ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="使用名称" prop="useName">
        <el-input v-model="queryParams.useName" placeholder="请输入使用名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="设备型号" prop="deviceModel">
        <el-input v-model="queryParams.deviceModel" placeholder="请输入设备型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备批次" prop="deviceLot">
        <el-input v-model="queryParams.deviceLot" placeholder="请输入设备批次" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备厂商" prop="deviceOem">
        <el-input v-model="queryParams.deviceOem" placeholder="请输入设备厂商" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['iotoperation:DeviceTypeUse:add']" type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['iotoperation:DeviceTypeUse:add']" type="warning" plain icon="el-icon-set-up" size="mini" @click="handleTypeConf">模板配置</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['iotoperation:DeviceTypeUse:edit']" >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['iotoperation:DeviceTypeUse:remove']" >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['iotoperation:DeviceTypeUse:export']" >导出</el-button>
      </el-col> -->
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="DeviceTypeUseList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" width="50" />
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键ID" align="center" prop="id" /> -->
      <!-- <el-table-column label="设备大类ID" align="center" prop="deviceTypeId" /> -->
      <el-table-column label="使用名称" align="center" prop="useName" />
      <!-- <el-table-column label="图标" align="center" prop="useIcon" /> -->
      <el-table-column label="适用房间" align="center" prop="useRoom">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.iot_install_room" :value="scope.row.useRoom" />
        </template>
      </el-table-column>
      <el-table-column label="适用位置" align="center" prop="useLocation">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.iot_install_location" :value="scope.row.useLocation" />
        </template>
      </el-table-column>
      <el-table-column label="数据通道" align="center" prop="dataChannel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.iot_data_channel" :value="scope.row.dataChannel" />
        </template>
      </el-table-column>
      <el-table-column label="设备型号" align="center" prop="deviceModel" />
      <el-table-column label="设备批次" align="center" prop="deviceLot" />
      <el-table-column label="设备厂商" align="center" prop="deviceOem" />
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermi="['iotoperation:DeviceTypeUse:edit']" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button v-hasPermi="['iotoperation:DeviceTypeUse:edit']" size="mini" type="text" icon="el-icon-set-up" @click="handleUseConf(scope.row)">配置</el-button>
          <el-button v-hasPermi="['iotoperation:DeviceTypeUse:remove']" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改设备使用类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备大类" prop="deviceTypeId">
          <el-input v-show="false" v-model="form.deviceTypeId" />
          <el-input v-model="form.deviceTypeName" :readonly="true" />
        </el-form-item>
        <el-form-item label="使用名称" prop="useName">
          <el-input v-model="form.useName" placeholder="请输入使用名称" />
        </el-form-item>
        <el-form-item label="适用房间" prop="useRoom">
          <el-select v-model="form.useRoom" style="width:100%" placeholder="请选择" clearable>
            <el-option
              v-for="dict in dict.type.iot_install_room"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="适用位置" prop="useLocation">
          <el-select v-model="form.useLocation" style="width:100%" placeholder="请选择" clearable>
            <el-option
              v-for="dict in dict.type.iot_install_location"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="图标" prop="useIcon">
          <el-input v-model="form.useIcon" placeholder="请输入图标" />
        </el-form-item> -->
        <el-form-item label="数据通道" prop="dataChannel">
          <el-select v-model="form.dataChannel" style="width:100%" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.iot_data_channel"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备型号" prop="deviceModel">
          <el-input v-model="form.deviceModel" placeholder="请输入设备型号" />
        </el-form-item>
        <el-form-item label="设备批次" prop="deviceLot">
          <el-input v-model="form.deviceLot" placeholder="请输入设备批次" />
        </el-form-item>
        <el-form-item label="设备厂商" prop="deviceOem">
          <el-input v-model="form.deviceOem" placeholder="请输入设备厂商" />
        </el-form-item>
        <!-- <el-form-item label="删除标识" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标识" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改设备使用类型对话框 -->
    <el-dialog :title="confTitle" :visible.sync="confOpen" fullscreen append-to-body>
      <device-type-conf ref="DeviceTypeConf" />
    </el-dialog>
  </div>
</template>

<script>
import { listDeviceTypeUse, getDeviceTypeUse, delDeviceTypeUse, addDeviceTypeUse, updateDeviceTypeUse } from "@/api/iotoperation/DeviceTypeUse";
import DeviceTypeConf from "../DeviceTypeConf/index";

export default {
  name: "DeviceTypeUse",
  components: { DeviceTypeConf },
  dicts: ["iot_install_room", "iot_install_location", "iot_data_channel"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备使用类型表格数据
      DeviceTypeUseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceTypeId: null,
        useName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        useName: [{ required: true, message: "使用名称必须填写", trigger: "blur" }],
        dataChannel: [{ required: true, message: "数据通道必须选择", trigger: "blur" }]
      },
      deviceTypeId: null,
      deviceTypeName: null,
      // 弹出层标题
      confTitle: "",
      // 是否显示弹出层
      confOpen: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 父组件触发刷新 */
    loadDataByType(row) {
      if (row) {
        this.deviceTypeId = row.id;
        this.deviceTypeName = row.typeName;
        this.queryParams.deviceTypeId = row.id;
      } else {
        this.deviceTypeId = null;
        this.deviceTypeName = null;
        this.queryParams.deviceTypeId = null;
      }

      this.getList();
    },
    /** 查询设备使用类型列表 */
    getList() {
      this.loading = true;
      listDeviceTypeUse(this.queryParams).then(response => {
        this.DeviceTypeUseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceTypeId: this.deviceTypeId,
        deviceTypeName: this.deviceTypeName,
        useName: null,
        useRoom: null,
        useLocation: null,
        dataChannel: null,
        useIcon: null,
        deviceModel: null,
        deviceLot: null,
        deviceOem: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      if (this.deviceTypeId) {
        this.reset();
        this.open = true;
        this.title = "添加设备使用类型";
      } else {
        this.$message.error("请先在左边选择一个设备分类");
      }
    },
    handleTypeConf() {
      if (this.deviceTypeId) {
        this.confTitle = "设备类型参数配置";
        this.confOpen = true;
        this.$nextTick(() => {
          this.$refs.DeviceTypeConf.loadPage("iot_device_type", this.deviceTypeId, this.deviceTypeName);
        });
      } else {
        this.$message.error("请先在左边选择一个设备分类");
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDeviceTypeUse(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备使用类型";
      });
    },
    /** 配置按钮操作 */
    handleUseConf(row) {
      this.confTitle = "设备使用类型参数配置";
      this.confOpen = true;
      this.$refs.DeviceTypeConf.loadPage("iot_device_type_use", row.useName, row.id);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDeviceTypeUse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeviceTypeUse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备使用类型编号为"' + ids + '"的数据项？').then(function() {
        return delDeviceTypeUse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download("iotoperation/DeviceTypeUse/export", {
        ...this.queryParams
      }, `DeviceTypeUse_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
